/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package net.qunzhi.modules.job.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.qunzhi.common.annotation.SysLog;
import net.qunzhi.common.utils.PageUtils;
import net.qunzhi.common.utils.R;
import net.qunzhi.common.validator.ValidatorUtils;
import net.qunzhi.modules.job.entity.ScheduleJobEntity;
import net.qunzhi.modules.job.query.ScheduleJobQuery;
import net.qunzhi.modules.job.service.ScheduleJobService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 定时任务
 *
 * @author Mark sunlightcs@gmail.com
 */
@Api(tags = "定时任务")
@RestController
@RequestMapping("/sys/schedule")
public class ScheduleJobController {
    @Autowired
    private ScheduleJobService scheduleJobService;

    @ApiOperation("定时任务列表")
    @GetMapping("/list")
    @RequiresPermissions("sys:schedule:list")
    public R list(ScheduleJobQuery queryForm) {
        PageUtils page = scheduleJobService.queryPage(queryForm);

        return R.ok().put("page", page);
    }

    /**
     * 定时任务信息
     */
    @ApiOperation("定时任务信息")
    @RequestMapping("/info/{jobId}")
    @RequiresPermissions("sys:schedule:info")
    public R info(@PathVariable("jobId") Long jobId) {
        ScheduleJobEntity schedule = scheduleJobService.getById(jobId);

        return R.ok().put("schedule", schedule);
    }

    /**
     * 保存定时任务
     */
    @ApiOperation("定时任务保存")
    @SysLog("保存定时任务")
    @RequestMapping("/save")
    @RequiresPermissions("sys:schedule:save")
    public R save(@RequestBody ScheduleJobEntity scheduleJob) {
        ValidatorUtils.validateEntity(scheduleJob);

        scheduleJobService.saveJob(scheduleJob);

        return R.ok();
    }

    /**
     * 修改定时任务
     */
    @ApiOperation("定时任务修改")
    @SysLog("修改定时任务")
    @RequestMapping("/update")
    @RequiresPermissions("sys:schedule:update")
    public R update(@RequestBody ScheduleJobEntity scheduleJob) {
        ValidatorUtils.validateEntity(scheduleJob);

        scheduleJobService.update(scheduleJob);

        return R.ok();
    }

    /**
     * 删除定时任务
     */
    @ApiOperation("定时任务删除")
    @SysLog("删除定时任务")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:schedule:delete")
    public R delete(@RequestBody Long[] jobIds) {
        scheduleJobService.deleteBatch(jobIds);

        return R.ok();
    }

    /**
     * 立即执行任务
     */
    @ApiOperation("定时任务执行")
    @SysLog("立即执行任务")
    @RequestMapping("/run")
    @RequiresPermissions("sys:schedule:run")
    public R run(@RequestBody Long[] jobIds) throws SchedulerException {
        scheduleJobService.run(jobIds);

        return R.ok();
    }

    /**
     * 暂停定时任务
     */
    @ApiOperation("定时任务暂替")
    @SysLog("暂停定时任务")
    @RequestMapping("/pause")
    @RequiresPermissions("sys:schedule:pause")
    public R pause(@RequestBody Long[] jobIds) {
        scheduleJobService.pause(jobIds);

        return R.ok();
    }

    /**
     * 恢复定时任务
     */
    @ApiOperation("定时任务恢复")
    @SysLog("恢复定时任务")
    @RequestMapping("/resume")
    @RequiresPermissions("sys:schedule:resume")
    public R resume(@RequestBody Long[] jobIds) {
        scheduleJobService.resume(jobIds);

        return R.ok();
    }

}
